package com.JZ.QM.service.impl;

import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;

import com.JZ.QM.common.dao.ProYieldDao;
import com.JZ.QM.common.entity.ProYieldEntity;
import com.JZ.QM.service.ProYieldService;

import javax.annotation.Resource;

/**
 *
 *
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020-06-14 11:19:36
 */
@Service
public class ProYieldServiceImpl extends ServiceImpl<ProYieldDao, ProYieldEntity> implements ProYieldService {
    @Resource
    ProYieldDao proYieldDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<ProYieldEntity> page = this.page(
                new Query<ProYieldEntity>().getPage(params),
                new QueryWrapper<ProYieldEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据产品编码获取产品的收益信息
     * @param proTsCode 产品的代码
     * @return
     */
    @Override
    public HashMap<String, Object> getProIncomes(String proTsCode) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //交易日期列表
        String[] tradeDates;
        //收益信息列表
        BigDecimal[] proIncomes;
        //产品的收益索引
        int proIndex=0;
        //产品的收益信息与交易日期列表
        HashMap<String,Object> proResult = new HashMap<>();
        //交易日期的数量
        int size=0;
        //产品收益信息列表
        ArrayList<ProYieldEntity> proYieldEntities;

        queryWrapper.select("pro_yield_money","pro_trade_date")
                .eq("pro_ts_code",proTsCode);
        queryWrapper.orderByAsc("pro_trade_date");

        proYieldEntities= (ArrayList) proYieldDao.selectList(queryWrapper);
        size=proYieldEntities.size();
        tradeDates=new String[size];
        proIncomes=new BigDecimal[size];
        //筛选组合的收益信息
        for (Iterator<ProYieldEntity> it = proYieldEntities.iterator(); it.hasNext(); ) {
            ProYieldEntity iter = it.next();
            proIncomes[proIndex]=iter.getProYieldMoney();
            tradeDates[proIndex]=iter.getProTradeDate();
            proIndex++;
        }
        proResult.put("proIncome",proIncomes);
        proResult.put("proTradeDate",tradeDates);
        return proResult;
    }

}